package com.yss.reportworld.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.domain.YssResponse;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.security.annotation.InnerAuth;
import com.yss.common.security.utils.DictUtils;
import com.yss.reportworld.domain.Datasql;
import com.yss.reportworld.domain.SqlScript;
import com.yss.reportworld.domain.Table;
import com.yss.reportworld.enums.ReportWorldEnum;
import com.yss.reportworld.service.*;
import com.yss.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 报表世界数据平台首页统计项
 */
@RestController
@RequestMapping("/statisticsReportWorld")
public class StatisticsReportWorldController extends BaseController {
    @Autowired
    private ITableService tableService;
    @Autowired
    private IDatasqlService datasqlService;
    @Autowired
    public IDemandBugInfoService demandBugInfoService;
    @Autowired
    private ISqlScriptService sqlScriptService;

    /**
     * 还原版本可以选择的版本号
     *
     * @return
     */
    @InnerAuth
    @PostMapping("/getVersionList")
    public YssResponse<List<String>> getVersionList() {
        List<SysDictData> dicvalueList = DictUtils.getDictCache(ReportWorldEnum.REPORT_VERSION.getCode());
        if (CollectionUtils.isEmpty(dicvalueList)) {
            return null;
        }
        List<String> list = this.getVersionListInfo();
        if (CollectionUtils.isEmpty(dicvalueList)) {
            return null;
        }
        List<String> dicvalueListnew = new ArrayList<>();
        for (String version : list) {
            dicvalueListnew.add(version.split("_")[1]);
        }
        return YssResponse.ok(dicvalueListnew);
    }

    /**
     * 获取平台首页要展示的版本号
     *
     * @return
     */
    private List<String> getVersionListInfo() {
        List<SysDictData> dicvalueList = sqlScriptService.getReductionVersion(true);
        //DictUtils.getDictCache(ReportWorldEnum.REPORT_VERSION.getCode());
        if (CollectionUtils.isEmpty(dicvalueList)) {
            return null;
        }
        List<String> dicvalueListnew = new ArrayList<>();
        for (int i = dicvalueList.size() - 1; i >= 0; i--) {  // 获取每个版本的数据信息
            if (StringUtils.isNull(dicvalueList.get(i)) || StringUtils.isEmpty(dicvalueList.get(i).getDictValue())) {
                continue;
            }
            dicvalueListnew.add(dicvalueList.get(i).getDictValue());
        }
        return dicvalueListnew;
    }

    /**
     * 报表世界表结构，脚本，基础数据 条数
     *
     * @return
     * @throws Exception
     */
    @InnerAuth
    @PostMapping("/getCount")
    public YssResponse<Map<String, Long>> getCount() {
        Map<String, Long> map = new HashMap<>();
        Long countDatasql = datasqlService.count();
        map.put("countDatasql", countDatasql);
        Long countTable = tableService.count();
        map.put("countTable", countTable);
        Long countSqlScript = sqlScriptService.count();
        map.put("countSqlScript", countSqlScript);
        return YssResponse.ok(map);
    }


    /**
     * 报表世界表结构，脚本，基础数据 条数
     *
     * @return
     * @throws Exception
     */
    @InnerAuth
    @PostMapping("/getCountVersion")
    public YssResponse<Map<String, List<Long>>> getCountVersion() {
        Map<String, List<Long>> map = new HashMap<>();
        List<Long> listSqlScript = new ArrayList<>();
        List<Long> listTable = new ArrayList<>();
        List<Long> listDatasql = new ArrayList<>();
        List<String> dicvalueList = this.getVersionListInfo();
        for (String version : dicvalueList) {  // 获取每个版本的数据信息
            QueryWrapper<SqlScript> wrapperSqlScript = new QueryWrapper<>();
            wrapperSqlScript.eq("VERSION", version);
            listSqlScript.add(sqlScriptService.count(wrapperSqlScript));

            QueryWrapper<Table> wrapperTable = new QueryWrapper<>();
            wrapperTable.eq("VERSION", version);
            listTable.add(tableService.count(wrapperTable));

            QueryWrapper<Datasql> wrapperDataSql = new QueryWrapper<>();
            wrapperDataSql.eq("VERSION", version);
            listDatasql.add(datasqlService.count(wrapperDataSql));
        }
        map.put("sqlScript", listSqlScript);
        map.put("table", listTable);
        map.put("dataSql", listDatasql);
        return YssResponse.ok(map);
    }

    /**
     * 首页公告信息
     *
     * @return
     * @throws Exception
     */
    @InnerAuth
    @PostMapping("/getNoticeInfo")
    public YssResponse<String> getNoticeInfo() {
        return YssResponse.ok("");
    }
}
